package top.gaui.nurse.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;


import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;
import top.gaui.nurse.entity.Nurse;

import top.gaui.nurse.jjwt.JwtUtils;
import top.gaui.nurse.repository.NurseRepository;
import top.gaui.nurse.result.Result;
import top.gaui.nurse.result.ResultCode;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


/**
 * @author Fujie_Zeng
 * @version 1.0
 * @date 2020/4/7 19:23
 */
@RestController
@RequestMapping("/nurse")
public class NurseHandler {
    @Autowired
    private NurseRepository nurseRepository;

    @Autowired
    private JwtUtils jwtUtils;

    @GetMapping("/findAll/{page}/{size}")
    public Page<Nurse> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        PageRequest request = PageRequest.of(page-1,size);
        return nurseRepository.findAll(request);
    }

    @PutMapping("/edit")
    public String edit(@RequestBody Nurse nurse){
        Nurse result=nurseRepository.save(nurse);
        if (result != null){return "success";}
        else {return "false";}
    }

    @PostMapping("/findByNum/{num}")
    public Optional<Nurse> findById(@PathVariable("num") Integer num){
        return nurseRepository.findById(num);
    }

    @PostMapping("/save")
    public String save(@RequestBody Nurse nurse){
        Nurse result=nurseRepository.save(nurse);
        if (result != null){return "success";}
        else {return "false";}
    }

    @PostMapping("/login/{num}/{pwd}")
    public Result login(@PathVariable("num")Integer num, @PathVariable("pwd") String pwd){
        String t="护士长";
         Nurse nurse=nurseRepository.findByNumAndPwd(num,pwd);
         if(nurse !=null&&nurse.getPwd().equals(pwd)&&nurse.getTitle().equals(t))
         {
             String token= jwtUtils.creatJwt(nurse.getNum());
             nurse.setPwd(null);
             return new Result(ResultCode.SUCCESS,token,nurse);
         }
         else{
             return new Result(ResultCode.FAIL);
         }

    }

    @PostMapping ({"/find/{name}/{department}/{page}/{size}","/find/{department}/{page}/{size}"})
   public  Page<Nurse> findAllByName(@PathVariable(required = false)String name,@PathVariable String department,@PathVariable Integer page, @PathVariable Integer size) {
        Specification<Nurse> specification = new Specification<Nurse>() {
            @Override
            public Predicate toPredicate(Root<Nurse> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (name !=null) {
                    predicateList.add(criteriaBuilder.like(root.get("name").as(String.class), "%"+ name+"%"));
                }
                if(department != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("department").as(String.class),department));
                }

                Predicate[] pre = new Predicate[predicateList.size()];
                criteriaQuery.orderBy(criteriaBuilder.asc(root.get("num")));
                return criteriaQuery.where(predicateList.toArray(pre)).getRestriction();
            }
        };
        PageRequest request = PageRequest.of(page-1,size);
        return nurseRepository.findAll(specification, request);


}

    @DeleteMapping("/delete/{num}")
    public String deleteById(@PathVariable("num")Integer num){
        nurseRepository.deleteById(num);
        return "success";
    }

}

